package cn.xtcode.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

/**
 * @Author xt-code
 * @Description: 策略路由抽象类
 * @Create 2025/5/6 11:38
 */
public abstract class AbstractStrategyRouter<T, D, R> implements StrategyMapper<T, D, R>, StrategyHandler<T, D, R> {

    @Getter
    @Setter
    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception {

        //执行路由信息。通过映射器，得到处理器
        StrategyHandler<T, D, R> strategyHandler = get(requestParameter, dynamicContext);

        //执行下一个策略
        if(null != strategyHandler) return strategyHandler.apply(requestParameter, dynamicContext);

        //null
        return defaultStrategyHandler.apply(requestParameter, dynamicContext);
    }
}
